package pe.gob.foncodes.ssa.bs.common.util;

import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import net.sf.jasperreports.engine.JRExporter;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JRReport;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.export.JRPrintServiceExporter;
import net.sf.jasperreports.engine.export.JRPrintServiceExporterParameter;


public class PrintServiceExporter implements JRExporterStrategy {


    public JRExporter export(SerializedList<JasperPrint> jasperPrintList, HttpServletResponse response) throws IOException {

        JRPrintServiceExporter exporter = new JRPrintServiceExporter();
        exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST, jasperPrintList);
        exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, response.getOutputStream());
        exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PAGE_DIALOG, Boolean.FALSE);
        exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PRINT_DIALOG, Boolean.FALSE);
        float w = 0;
        float h = 0;
        for (JasperPrint temp : jasperPrintList) {
            if (w < temp.getPageWidth()) {
                w = temp.getPageWidth();
            }
            h += temp.getPageHeight();
        }
        // Px / 72 a INCH luego * 2.54 a CM
        w = w / 72f * 2.54f;
        h = h / 72f * 2.54f;
     //   exporter.getParameters().putAll(JasperReportUtil.preparePrinterJob("", JRReport.LANGUAGE_GROOVY, 1, w, h));
        return new PdfExporterDecorator(exporter, jasperPrintList, response);
    }


    public String getMimeType() {
        return "application/pdf";
    }

    @Override
    public String getFileExtension() {
        return "";
    }
}